package com.hskn.hss.module.cleardetail.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.cleardetail.entity.ClearDetail;
import com.hskn.hss.module.cleardetail.mapper.ClearDetailMapper;
import com.hskn.hss.module.cleardetail.service.IClearDetailService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-20
 */
@Service
public class ClearDetailServiceImpl extends ServiceImpl<ClearDetailMapper, ClearDetail> implements IClearDetailService {

    @Resource
    private ClearDetailMapper clearDetailMapper;

    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        JSONObject json = new JSONObject();
        QueryWrapper<ClearDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(ClearDetail::getCreateTime);
        String workPlanId = params.getString("workPlanId");
        if (StringUtils.isNotEmpty(workPlanId)) {
            queryWrapper.lambda().eq(ClearDetail::getWorkPlanId, workPlanId);
        }
        String qydId = params.getString("qydId");
        if (StringUtils.isNotEmpty(qydId)) {
            queryWrapper.lambda().eq(ClearDetail::getQydId, qydId);
        }
        String qydName = params.getString("qydName");
        if (StringUtils.isNotEmpty(qydName)) {
            queryWrapper.lambda().like(ClearDetail::getQydName, qydName);
        }
        String carId = params.getString("carId");
        if (StringUtils.isNotEmpty(carId)) {
            queryWrapper.lambda().eq(ClearDetail::getCarId, carId);
        }
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.lambda().between(ClearDetail::getTime, startDate, endDate);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<ClearDetail> clearDetailPage = new Page<>(page, size, true);
        IPage<ClearDetail> iPage = clearDetailMapper.selectPage(clearDetailPage, queryWrapper);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("list", iPage.getRecords());
        return json;
    }

    @Override
    public JSONObject collectList(JSONObject params) throws HssException {
        JSONObject res = new JSONObject();
        Long page = (StringUtils.isEmpty(params.getLong("page")) || params.getLong("page") == 0) ? 1 : params.getLong("page");
        Long size = (StringUtils.isEmpty(params.getLong("size")) || params.getLong("size") == 0) ? 10 : params.getLong("size");
        params.put("offset", (page - 1) * size);
        params.put("limit", size);
        List<Map<String, Object>> list = clearDetailMapper.collectList(params);
        long count = clearDetailMapper.collectListCount(params);
        res.put("list", list);
        res.put("total", count);
        res.put("current", page);
        res.put("pages", (int) Math.ceil(count / size));
        res.put("size", list.size());
        return res;
    }

    @Override
    public List<Map<String, Object>> countByDay(JSONObject params) throws HssException {

        List<Map<String, Object>> count = clearDetailMapper.countByDay(params);
        return count;
    }

}
